package cn.edu.neusoft.myappos.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.myappos.Activity.UserActivity;
import cn.edu.neusoft.myappos.Bean.UserBean;
import cn.edu.neusoft.myappos.R;

public class UserAdapter extends RecyclerView.Adapter {
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局
    private List<UserBean> list;//向rv中填充的数据
    //自定义 构造方法
    public UserAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    //自定义 ViewHoder子类，容纳item视图
    public  class ViewHolder extends  RecyclerView.ViewHolder {
        ImageView imageView;
        TextView name,pos,year;
        Button button;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView);
            name=itemView.findViewById(R.id.Name);
            pos=itemView.findViewById(R.id.pos);
            year=itemView.findViewById(R.id.TextView24);
        }
    }
    //自定义 设置数据list
    public void setList(List<UserBean> list){
        this.list=list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    //为每个item填充数据，设置点击事件
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate
                (R.layout.user_item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        //取数据
        final UserBean bean=list.get(position);
        if(bean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.imageView.setImageResource(bean.getPic());
        viewHolder.name.setText(bean.getName());
        viewHolder.pos.setText(bean.getPos());
        viewHolder.year.setText(bean.getYear());
//        viewHolder.button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                list.remove(position);
//                notifyDataSetChanged();
//            }
//        });
    }

    @Override
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }

}
